package com.example.expinterview.controller;

import com.example.expinterview.entity.EiUser;
import com.example.expinterview.entity.ResultBody;
import com.example.expinterview.service.IEiUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @classname: UserController
 * @description: TODO
 * @author: kaiser
 * @date: 2021/10/18 22:03
 * @version: 1.0
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
    private final IEiUserService service;

    @Autowired
    public UserController(IEiUserService service) {
        this.service = service;
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "创建用户")
    public ResultBody save(@RequestBody EiUser user) {
        return service.createUser(user);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户信息")
    public ResultBody update(@RequestBody EiUser user) {
        return ResultBody.status(service.updateById(user));
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据ID获取用户信息")
    public ResultBody get(@PathVariable Long id) {
        return ResultBody.success(service.getById(id));
    }

    @PostMapping("/getByOpenid")
    @ApiOperation(value = "根据openid获取用户信息")
    public ResultBody getByOpenid(String openid) {
        return service.selectByOpenid(openid);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除用户")
    public ResultBody delete(@PathVariable Long id) {
        return ResultBody.status(service.removeById(id));
    }
}
